package com.jrx.anytxn.param.controller.limit;

import com.google.common.collect.Maps;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.param.dto.limit.CreditLimitTypeReq;
import com.jrx.anytxn.param.dto.limit.CreditLimitTypeRes;
import com.jrx.anytxn.param.plugins.DeleteLog;
import com.jrx.anytxn.param.plugins.DeleteLogHelper;
import com.jrx.anytxn.param.service.limit.ICreditLimitTypeService;
import io.swagger.annotations.*;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author by yf
 * @Classname CreditLimitTypeController
 * @Description 额度节点表
 * @Date 2019/6/12 0012 下午 21:09
 */
@Api(tags = "与页面相关的额度节点表参数API接口")
@RestController
@RequestMapping("/param/limitTypes")
public class CreditLimitTypeController {
    private static final Logger logger = LoggerFactory.getLogger(CreditLimitTypeController.class);

    @Autowired
    private ICreditLimitTypeService iCreditLimitTypeService;
    @Autowired
    private InfoService infoService;

    @ApiOperation(value = "分页查询额度节点表信息", notes = "分页查询额度节点表信息")
    @GetMapping("/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "页大小", required = true)})
    public TxnRespResult<TxnPage<CreditLimitTypeRes>> getAllLimitTypeByPage(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) {
        TxnRespResult<TxnPage<CreditLimitTypeRes>> result = new TxnRespResult<>();
        TxnPage<CreditLimitTypeRes> response;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            response = iCreditLimitTypeService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和节点ID查询额度类型信息", notes = "根据租户ID和节点ID查询额度类型信息")
    @GetMapping(value = "/{tenantId}/{limitId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "limitId", value = "节点ID", required = true)
    })
    public TxnRespResult<CreditLimitTypeRes> getLimitTypeById(@PathVariable String tenantId, @PathVariable String limitId) {
        TxnRespResult<CreditLimitTypeRes> result = new TxnRespResult<>();
        CreditLimitTypeRes res;
        try {
            res = iCreditLimitTypeService.findLimitTypeByTenIdAndLimitId(String.valueOf(infoService.getUser().getProjectId()), limitId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID和节点ID删除额度类型信息", notes = "根据租户ID和节点ID删除额度类型信息")
    @DeleteMapping(value = "/{tenantId}/{limitId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId", value = "租户ID", required = true),
            @ApiImplicitParam(name = "limitId", value = "节点ID", required = true)
    })
    public TxnRespResult<Boolean> removeLimitTypeById(@PathVariable String tenantId, @PathVariable String limitId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            DeleteLog deleteLog = new DeleteLog(infoService.getUser().getUserName());
            DeleteLogHelper.setDelleteHistory(deleteLog);

            flag = iCreditLimitTypeService.removeTenIdAndLimitId(String.valueOf(infoService.getUser().getProjectId()), limitId);
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        } finally {
            DeleteLogHelper.clearLocalHistory();
        }
        return result;
    }

    @ApiOperation(value = "新增额度类型信息", notes = "新增额度类型信息")
    @PostMapping("/")
    public TxnRespResult<CreditLimitTypeRes> createLimitType(@RequestBody CreditLimitTypeReq creditLimitTypeReq) {
        TxnRespResult<CreditLimitTypeRes> result = new TxnRespResult<>();
        CreditLimitTypeRes response;
        try {
            creditLimitTypeReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            creditLimitTypeReq.setUpdateBy(infoService.getUser().getUserName());
            response = iCreditLimitTypeService.addCustomerLimitInfoTable(creditLimitTypeReq);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑额度类型信息", notes = "编辑额度类型信息")
    @PutMapping("/")
    public TxnRespResult<CreditLimitTypeRes> modifyLimitType(@RequestBody CreditLimitTypeReq creditLimitTypeReq) {
        TxnRespResult<CreditLimitTypeRes> result = new TxnRespResult<>();
        CreditLimitTypeRes response;
        try {
            creditLimitTypeReq.setUpdateBy(infoService.getUser().getUserName());
            response = iCreditLimitTypeService.modifyCustomerLimitInfoTable(creditLimitTypeReq);
            result.getSuccess(response);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "获得额度节点表信息", notes = "获得额度节点表信息")
    @GetMapping(value = "/list")
    public TxnRespResult<HashMap<String, List<CreditLimitTypeRes>>> getAllList() {
        TxnRespResult<HashMap<String, List<CreditLimitTypeRes>>> result = new TxnRespResult<>();

        String tenantId = String.valueOf(infoService.getUser().getProjectId());
        List<CreditLimitTypeRes> allChannel = iCreditLimitTypeService.findAllLimitTypeByTenId(tenantId);

        HashMap<String, List<CreditLimitTypeRes>> sysCodeMap = Maps.newHashMap();
        sysCodeMap.put("listData", allChannel);
        return result.getSuccess(sysCodeMap);
    }

    @ApiOperation(value = "根据租户ID和机构号获取有效节点id列表信息", notes = "根据租户ID和机构号获取节点id列表信息")
    @GetMapping(value = "/list/orgId/{orgId}")
    @ApiImplicitParam(name = "orgId", value = "机构号", required = true)
    public TxnRespResult<HashMap<String, List<CreditLimitTypeRes>>> getLimitTypeListByOrgId(@PathVariable(value = "orgId") String orgId) {
        TxnRespResult<HashMap<String, List<CreditLimitTypeRes>>> result = new TxnRespResult<>();

        List<CreditLimitTypeRes> allLimitType;
        try {
            String tenantId =String.valueOf(infoService.getUser().getProjectId());
            allLimitType = iCreditLimitTypeService.findAllLimitTypeByOrgIdAndTenId(orgId, tenantId);
            HashMap<String, List<CreditLimitTypeRes>> sysCodeMap = Maps.newHashMap();
            sysCodeMap.put("listData", allLimitType);
            result.getSuccess(sysCodeMap);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID，节点ID和机构号查询额度类型信息", notes = "根据租户ID节点ID和机构号查询额度类型信息")
    @GetMapping(value = "/limitId/{limitId}/orgId/{orgId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limitId", value = "额度节点ID", required = true),
            @ApiImplicitParam(name = "orgId", value = "机构号", required = true)
    })
    public TxnRespResult<CreditLimitTypeRes> getByLimitIdAndOrgId(@PathVariable String limitId, @PathVariable String orgId) {
        TxnRespResult<CreditLimitTypeRes> result = new TxnRespResult<>();
        CreditLimitTypeRes res;
        try {
            String tenantId =String.valueOf(infoService.getUser().getProjectId());
            res = iCreditLimitTypeService.findByLimitIdAndOrgIdAndTenId(limitId, orgId, tenantId);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(), e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }


}
